package com.changgou.service.goods.service.impl;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changgou.goods.pojo.*;
import com.changgou.service.goods.dao.SpuMapper;
import com.changgou.service.goods.service.*;
import com.changgou.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * Package: com.changgou.goods.service.impl
 * ClassName: SpuServiceImpl
 * Description:
 *
 * @Author : 福森森
 * @Create : 2024/8/28 20:00
 */
@Service
public class SpuServiceImpl extends ServiceImpl<SpuMapper, Spu> implements SpuService {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private CategoryBrandService categoryBrandService;

    @Transactional
    @Override
    public void add(Goods goods) {
        Spu spu = goods.getSpu();
        long spuId = idWorker.nextId();
        spu.setId(Convert.toStr(spuId));
        spu.setIsDelete("0");
        spu.setIsMarketable("0");
        spu.setStatus("0");
        this.save(spu);

        saveSkuList(goods);
    }


    private void saveSkuList(Goods goods) {
        //获取spu对象
        Spu spu = goods.getSpu();
        //当前时间
        LocalDateTime date = LocalDateTime.now();
        //获取品牌对象
        Brand brand = brandService.findById(spu.getBrandId());
        //获取分类对象
        Category category = categoryService.findById(spu.getCategory3Id());


        CategoryBrand categoryBrand = new CategoryBrand();
        categoryBrand.setBrandId(spu.getBrandId());
        categoryBrand.setCategoryId(spu.getCategory3Id());
        int count = categoryBrandService.findByObjAndGetNum(categoryBrand);
        if (count == 0) {
            categoryBrandService.save(categoryBrand);
        }

        //获取sku集合对象
        List<Sku> skuList = goods.getSkuList();
        if (skuList != null && skuList.size() > 0) {
            for (Sku sku : skuList) {
                //设置sku主键Id
                sku.setId(Convert.toStr(idWorker.nextId()));
                //设置sku规格
                if (sku.getSpec() == null || "".equals(sku.getSpec())) {
                    sku.setSpec("{}");
                }
                //设置sku名称(商品名+规格)
                String name = spu.getName();
                //将规格json字符串转换成map
                Map<String, String> specMap = JSON.parseObject(sku.getSpec(), Map.class);
                if (specMap != null && specMap.size() > 0) {
                    for (String value : specMap.values()) {
                        name += "" + value;
                    }
                }
                sku.setName(name);//名称
                sku.setSpuId(spu.getId());
                sku.setCreateTime(date);
                sku.setUpdateTime(date);
                sku.setCategoryId(category.getId());
                sku.setCategoryName(category.getName());
                skuService.save(sku);
            }
        }

    }

    @Override
    public Goods findGoodsById(String id) {
        Spu spu = this.lambdaQuery().eq(Spu::getId, id).one();
        List<Sku> skuList = skuService.getListBySpuId(id);
        Goods goods = new Goods();
        goods.setSpu(spu);
        goods.setSkuList(skuList);
        return goods;
    }

    @Override
    public void updateByIdWiteObj(String id, Goods goods) {
        Spu spu = goods.getSpu();
        this.updateById(spu);
        skuService.removeBySpuId(spu.getId());
        saveSkuList(goods);
    }

    @Transactional
    @Override
    public void audit(String id) {
        Spu spu = this.lambdaQuery().eq(Spu::getId, id).one();
        if (spu == null) {
            throw new RuntimeException("当前商品不存在");
        }
        if ("1".equals(spu.getIsDelete())) {
            throw new RuntimeException("当前商品处于删除状态");
        }
        spu.setStatus("1");
        spu.setIsMarketable("1");
        this.updateById(spu);
    }

    @Transactional
    @Override
    public void pull(String id) {
        Spu spu = this.lambdaQuery().eq(Spu::getId, id).one();
        if (spu == null) {
            throw new RuntimeException("当前商品不存在");
        }
        if ("1".equals(spu.getIsDelete())) {
            throw new RuntimeException("当前商品处于删除状态");
        }
        spu.setStatus("0");
        this.updateById(spu);
    }

    @Override
    public void put(String id) {
        Spu spu = this.lambdaQuery().eq(Spu::getId, id).one();
        if (!spu.getStatus().equals("1")) {
            throw new RuntimeException("未通过审核的商品不能上架!");
        }
        spu.setIsMarketable("1");
        this.updateById(spu);
    }

    @Override
    public void deleteById(String id) {
        Spu spu = this.lambdaQuery().eq(Spu::getId, id).one();
        if (!spu.getIsMarketable().equals("0")) {
            throw new RuntimeException("必须先下架商品再删除");
        }
        spu.setIsDelete("1");
        spu.setStatus("0");
        this.updateById(spu);
    }

    @Override
    public void restore(String id) {
        Spu spu = this.lambdaQuery().eq(Spu::getId, id).one();
        if (!spu.getIsDelete().equals("1")) {
            throw new RuntimeException("当前商品未删除");
        }
        spu.setIsDelete("0");
        spu.setStatus("0");
        this.updateById(spu);
    }

    @Override
    public void realDelete(String id) {
        Spu spu = this.lambdaQuery().eq(Spu::getId, id).one();
        if (!spu.getIsDelete().equals("1")) {
            throw new RuntimeException("当前商品未删除");
        }
        this.removeById(id);
    }


}


















